import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../views/Home.vue";

const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/",
    name: "Home",
    component: Home,
    children: [

    ]
  },
  {
    path: "/index",
    name: "index",
    meta: {
      title: "首页",
    },
    component: () =>
      import(/* webpackChunkName: "index" */ "../pages/index/index.vue"),
  },
];

const router = createRouter({
  mode: "history",
  base: "lighthouse",
  history: createWebHashHistory(),
  routes,
});
// 生态资源申报流转平台
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title} | ${localStorage.getItem("platformName")}`;
  const role = localStorage.getItem("ms_username");
  next();
  // if (to.path !== '/index') {
  //     next('/index');
  // } else if (to.meta.permission) {
  //     // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
  //     role === 'admin'
  //         ? next()
  //         : next('/403');
  // } else {
  //     next();
  // }

  if (!role && to.path !== "/login") {
    next("/index");
  } else if (to.meta.permission) {
    // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
    role === "admin" ? next() : next("/403");
  } else {
    next();
  }
});

export default router;
